import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoginRoute extends StatefulWidget{
  @override
  State<StatefulWidget> createState()=>_LoginRouteState();
  
}

class _LoginRouteState extends State<LoginRoute>{
  String userName='';
  String passWord='';
  GlobalKey _formKey= new GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body:Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10)),
          image: DecorationImage(
            image: AssetImage('images/login.jpg'),
            fit: BoxFit.cover
          ),
          color: Colors.brown
        ),
        child:Form(
          key:_formKey,
          autovalidateMode: AutovalidateMode.onUserInteraction,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children:<Widget>[
              TextFormField(
                autofocus: true,
                decoration: InputDecoration(
                    labelText: "用户名",
                    hintText: "请输入用户名",
                    icon: Icon(Icons.person)
                ),
                validator: (v) {
                  return (v as String).trim().length > 0 ? null : "用户名不能为空";
                },
                onChanged: (val) =>{
                  userName=val
                },
              ),
              TextFormField(
                decoration: InputDecoration(
                    labelText: "密码",
                    hintText: "请输入密码",
                    icon: Icon(Icons.lock)
                ),
                obscureText: true,
                validator: (v) {
                  return (v as String).trim().length > 5 ? null : "密码不能少于6位";
                },
                onChanged: (val) =>{
                  passWord=val
                },
              ),
              Padding(
                padding: EdgeInsets.all(20),
                child: Row(
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        child: Text("登录"),
                        style: ButtonStyle(
                          fixedSize: MaterialStateProperty.all<Size>(Size.fromHeight(40))
                        ),
                        onPressed: () {
                          //在这里不能通过此方式获取FormState，context不对
                          //print(Form.of(context));
                            
                          // 通过_formKey.currentState 获取FormState后，
                          // 调用validate()方法校验用户名密码是否合法，校验
                          // 通过后再提交数据。 
                          if((_formKey.currentState as FormState).validate()){
                            //验证通过提交数据
                            print({userName,passWord});
                            if(userName!='admin'||passWord!='123456'){
                              
                            }
                            Navigator.pushNamed(context, '/home');
                          }
                        },
                      ),
                    )
                  ],
                ),
              )
            ]
          ),
        )
      )
    );
  }
  
}